package TOP101;

/**
 * @Question BM9
 * @Date 2022/11/3 20:34
 * @Solution
 */
public class BM9 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode fast = head;
        for (int i = 0; i < n + 1; i++) {
            if (fast == null) {
                return head.next;
            }
            fast = fast.next;
        }
        ListNode slow = head;
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return head;
    }
}
